package com.wb3.meta.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author 时间工具类
 * @Description //TODO raopeng
 * @Date 2021/1/8 19:58
 * @Param
 * @return
 **/
public class DateUtils {


    // 根据指定格式显示日期和时间
//    /** yyyy-MM-dd */
//    private static final DateTimeFormatter yyyyMMdd_EN = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//    /** yyyy-MM-dd HH */
//    private static final DateTimeFormatter yyyyMMddHH_EN = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
//    /** yyyy-MM-dd HH:mm */
//    private static final DateTimeFormatter yyyyMMddHHmm_EN = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    /** yyyy-MM-dd HH:mm:ss */
    public static final String yyyyMMddHHmmss_EN = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyyMMdd_CN = "yyyy年MM月dd日";
    /** 日期格式(yyyy-MM-dd) */
    public static final String yyyy_MM_dd_EN = "yyyy-MM-dd";
    /** HH:mm:ss */
    /**
     * @Author raopeng
     * @Description //TODO 校验时间是否为有效格式 ，  yyyy-mm-dd 格式
     * @Date 2021/1/8 19:59
     * @Param [sDate]
     * @return boolean
     **/
    public static boolean isValidDate(String sDate) {
        String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
        String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
                + "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
                + "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
                + "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
        if ((sDate != null)) {
            Pattern pattern = Pattern.compile(datePattern1);
            Matcher match = pattern.matcher(sDate);
            if (match.matches()) {
                pattern = Pattern.compile(datePattern2);
                match = pattern.matcher(sDate);
                return match.matches();
            }
            else {
                return false;
            }
        }
        return false;
    }


    public static String millonTime2Str(String time) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        if (time.length() == 10) {
            time += "000"; // 秒转毫秒
        }
        Date date = new Date();
        date.setTime(Long.parseLong(time));


        return date2Str(date, null);
    }

    public static String date2Str(Date date,String format) {
        format = StringUtils.isBlank(format) ? yyyyMMddHHmmss_EN : format;
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(date);
        return dateString;
    }
    public static String getCurDate() {
        return date2Str(new Date(), yyyyMMddHHmmss_EN);
    }
    /**
     * 按照默认formatStr的格式，转化dateTimeStr为Date类型 dateTimeStr必须是formatStr的形式
     *
     * @param dateTimeStr
     * @param formatStr
     * @return
     */
    public static Date getDate(String dateTimeStr, String formatStr) {
        try {
            if (dateTimeStr == null || dateTimeStr.equals("")) {
                return null;
            }
            SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
            Date d = formatter.parse(dateTimeStr);
            return d;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    public static Date getDate(String dateTimeStr) {


        return getDate(dateTimeStr, yyyy_MM_dd_EN);
    }
    public static long compareDateStr(String time1, String time2) {
        Date d1 = getDate(time1);
        Date d2 = getDate(time2);
        return d2.getTime() - d1.getTime();
    }

    public static String checkDateIsNull(Date date,String formatStr){
        String result = "";
        if(date != null){
            result = date2Str(date, formatStr);
        }
        return result;
    }

    /**
     * daysBetweenUsingJoda
     * 获取两个视角之间的天数，每满一天返回1
     *
     * @param time1
     * @param time2
     * @return the days between the two times
     */
    public static int daysBetweenUsingJoda(long time1, long time2) {
        return Days.daysBetween(new LocalDate(time1), new LocalDate(time2)).getDays();
    }

    /**
     * 是否跨周
     *
     * @param time1
     * @param time2
     * @return
     */
    static final long SEVEN_DAY_MILLI_SECOND = 7 * 24 * 60 * 60 * 1000;
    public static boolean beCrossWeekOnMonday(long time1, long time2) {
        LocalDate localDate1 = new LocalDate();
        LocalDate localDate2 = new LocalDate();

        if (localDate1.getWeekOfWeekyear() != localDate2.getWeekOfWeekyear()) {
            return true;
        }
        //解决不同的年返回的是相同的周的问题
        else if (Math.abs(time2 - time1) >= SEVEN_DAY_MILLI_SECOND)
        {
            return true;
        }

        return false;
    }

    /**
     * 是否跨月
     *
     * @param time1
     * @param time2
     * @return
     */
    public static boolean beCrossMonth(long time1, long time2) {
        LocalDate localDate1 = new LocalDate(time1);
        LocalDate localDate2 = new LocalDate(time2);

        if (localDate1.getMonthOfYear() != localDate2.getMonthOfYear()) {
            return true;
        }
        if (localDate1.getMonthOfYear() == localDate2.getMonthOfYear() && localDate1.getYear() != localDate2.getYear()){
            return true;
        }
        return false;
    }
}
